char a[5],b[5],t;
func(char*a,char*b){return*a-*b;}
main(flush,st,pair,anpair,n,i)
{
	for(scanf("%d\n",&n);n--;)
	{
		for(i=0;i<5;i++)
			scanf("%c%c ",a+i,b+i);
		for(flush=0,t=*b,i=0;i<5;i++)
		{
			if(a[i]=='T')a[i]='9'+1;
			if(a[i]=='J')a[i]='9'+2;
			if(a[i]=='Q')a[i]='9'+3;
			if(a[i]=='K')a[i]='9'+4;
			flush|=t!=b[i];
		}
		qsort(a,5,1,func);
		flush=!flush;
		printf("%d\n",flush);
	}
}
